﻿layui.config({
    base: "Scripts/"
}).use(['form', 'layer', 'jquery', 'laypage'], function () {
    var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		$ = layui.jquery,
		pageData = { "PageSize": 5, "PageCount": 0, "PageNumber": 0, "RowCount": 0 };

    //表格数据和分页
    function getList(that) {
        $.ajax({
            type: "POST",
            url: "/Official/OfficialEmp",
            data: that,
            dataType: "json",
            success: function (data) {
                var result = JSON.parse(data.Data);
                var usersData = result.DataList;

                pageData.PageCount = result.PageCount;
                pageData.PageNumber = result.PageNumber;
                pageData.PageSize = result.PageSize;
                pageData.RowCount = result.RowCount;
                //执行加载数据的方法
                renderDate(usersData);
                showPage(pageData);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert(textStatus);
            }
        });

        //渲染数据
        function renderDate(list) {

            var dataHtml = '';
            if (list != null) {
                layui.each(list, function (index, item) {
                    dataHtml += '<tr>'
                    + '<td>' + item.JobNumber + '</td>'
                    + '<td>' + item.Name + '</td>'
                    + '<td>' + item.Sex + '</td>'
                    + '<td>' + item.D_Name + '</td>'
                    + '<td>' + item.P_Name + '</td>'
                    + '<td>' + item.EntryTime + '</td>'
                    + '<td>' + item.Remark + '</td>'
                    + '<td>' + item.TrialPeriodDaty + '</td>'
                    + '<td>' + item.TrialPay + '</td>'
                    + '<td>' + item.days + '</td>'
                    + '<td>'
                    + '<a id="postId" class="layui-btn layui-btn-mini layui-btn-sm news_edit" data-id="' + item.JobNumber + '"><i class="iconfont icon-edit"></i>同意</a>'
                    + '</td>'
                    + '<td>'
                    + '<a id="postId" class="layui-btn layui-btn-mini layui-btn-sm news_edits" data-id="' + item.JobNumber + '"><i class="iconfont icon-edit"></i>不同意</a>'
                    + '</td>'

                    + '</tr>';
                });
            } else {
                dataHtml = '<tr><td colspan="8">暂无数据</td></tr>';
            }
            $(".news_content").html(dataHtml);   //渲染数据
            form.render(); //渲染表单
        }

    }
    //分页
    function showPage(page) {
        laypage.render({
            elem: 'page',
            count: page.RowCount,//得到总行数
            limit: page.PageSize,
            curr: page.PageNumber + 1,
            groups: 5, //连续显示分页数
            jump: function (obj, first) {
                if (!first) {//防止无限刷新
                    page.PageNumber = obj.curr - 1;
                    getList(page);
                }
            }
        });
    }

    $("body").on("click", ".news_edit", function () {

        var _this = $(this);
        alert(_this.attr("data-id"));
        layer.confirm('员工转正', { icon: 3, title: '提示信息' }, function (index) {
            var index = layer.msg('审批中，请稍候', { icon: 16, time: false, shade: 0.8 });
            $.ajax({
                type: "post",
                url: "/Official/Emp?id=" + _this.attr("data-id") + "",
                dataType: "json",
                success: function (result) {
                    var i = layer.msg(result.Message, { icon: 16, time: false, shade: 0.8 });
                    getList(pageData);
                    layer.close(i);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.alert(textStatus);
                }
            });
            layer.close(index);
        });
    });
    $("body").on("click", ".news_edits", function () {
        var _this = $(this);
        layer.confirm('员工转正', { icon: 3, title: '提示信息' }, function (index) {
            var index = layer.msg('审批中，请稍候', { icon: 16, time: false, shade: 0.8 });
            $.ajax({
                type: "post",
                async: false,
                url: "/Official/Emps?id=" + _this.attr("data-id") + "",
                data: { id: _this.attr("data-id") },
                success: function (result) {
                    var i = layer.msg(result.Message, { icon: 16, time: false, shade: 0.8 });
                    getList(pageData);
                    layer.close(i);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.alert(textStatus);
                }
            });
            layer.close(index);
        });
    });


    function Select() {
        var url = "/TransferBoss/Select";
        $.post(url, function (result) {
            var departments = JSON.parse(result.data);
            var deptSelect = $("#deptSelect");
            layui.each(departments, function (index, data) {
                var deptOption = $("<option value='" + data.D_Name + "'>" + data.D_Name + "</option>");
                deptSelect.append(deptOption);
            });
            form.render();
        });
    };

    $(function () {
        Select();
        getList(pageData);
        $('button').click(function () {
            pageData.D_NameR = $("select option:selected").attr("value");
            alert(JSON.stringify(pageData));
            getList(pageData);
        })
    });
})